package net.betou.trade.action;

import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.betou.common.util.TradeUtils;
import net.betou.common.web.RequestUtils;
import net.betou.common.web.ResponseUtils;
import net.betou.core.service.post.SmsSender;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.main.manager.CategoryMng;
import net.betou.trade.main.manager.ChannelMng;
import net.betou.trade.main.manager.UserGroupMng;
import net.betou.trade.main.manager.UserMng;
import net.betou.trade.main.manager.agencyorproxy.AdminMng;
import net.betou.trade.main.manager.agencyorproxy.ProxyMng;
import net.betou.trade.main.manager.role.AgencyRoleMng;
import net.betou.trade.web.ResponseUtil;
import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class AjaxAction{
	
	@RequestMapping( "/v_channel_sms.jspx" )
	public String getChannelSmsGroup( Integer id, HttpServletRequest request, HttpServletResponse response, ModelMap model ){
		
		model.addAttribute( "ajaxGroupList", groupMng.getAllowReadList( null, null, null, null ) );
		model.addAttribute( "channel", channelMng.findById( id ) );
		
		return "content/smsgroup";
	}
	
	/**
	 * 获取本次使用的数量
	 */
	@RequestMapping( "/v_sms_use.jspx" )
	public void getUseSmsNum( String smsGroups, HttpServletRequest request, HttpServletResponse response  ){
		
		ResponseUtil util = ResponseUtil.create( response );
		
		AgentAndAdmins current = TradeUtils.getAdmin( request ); // 获取当前管理员
		
		// 获取当前管理员剩余短信数量
		Integer currentSmsNum = TradeUtils.getCurrentAdminSmsNum( current, manager );
		
		if( StringUtils.isBlank( smsGroups ) ){
			util.addSuccessResponse( "当前剩余短信数量:" + currentSmsNum + "，本次操作将使用0条短信！" );
			util.renderJson( );
			return;
		}
		
		// 要发送的号码数量
		Set< String > mobiles = TradeUtils.getMobiles( userMng, proxyMng, agencyRoleMng, current, smsGroups );
		
		Integer num = 0;
		
		if( mobiles != null && !mobiles.isEmpty( ) ){
			num = mobiles.size( );
		}
		
		if( num > currentSmsNum ){
			util.addErrorResponse( "<span class='red'>短信数量不足，剩余数量:" + currentSmsNum + "，使用数量:" + num + "</span>");
			util.renderJson( );
			return;
		}
		
		util.addSuccessResponse( "当前剩余短信数量:" + currentSmsNum + "，本次操作将使用<span class='red'>" + num + "</span>条短信！" );
		util.renderJson( );
		return;
		
	}
	
	
	@RequestMapping( "/v_sms_surplus.jspx" )
	public void smsSurplus( HttpServletRequest request, HttpServletResponse response ){
		ResponseUtil util = ResponseUtil.create( response );
		util.addSuccessResponse( smsSender.getSmsNum( ).toString( ) );
		util.renderJson( );
		return;
	}
	
	@RequestMapping( "/v_list_category.jspx" )
	public void  listcategory( String term, HttpServletRequest request, HttpServletResponse response ){
		
		if( StringUtils.isNotBlank( term ) ){
			term = RequestUtils.parseParamUsingUTF8( term );
			
			AgentAndAdmins admin = TradeUtils.getAdmin( request );
			
			ResponseUtils.renderJson( response, JSONArray.fromObject( categoryMng.findListByName( admin, term ) ).toString( ) );
		}
	}
	@RequestMapping( "/v_check_category.jspx" )
	public void checkCategoryName( Boolean exist, String categoryName, String name, HttpServletRequest request, HttpServletResponse response  ){
		categoryName = RequestUtils.parseParamUsingUTF8( categoryName );
		
		if( StringUtils.isBlank( categoryName ) ){
			categoryName = RequestUtils.parseParamUsingUTF8( name ); // 如果categoryName 没有的话， 就使用name
		}
		
		if( StringUtils.isBlank( categoryName ) ){
			ResponseUtils.renderJson( response, Boolean.FALSE.toString( ) ); // 返回true的话，表示为不存在
			return;
		}
		
		boolean categoryExist = categoryMng.categoryExist( categoryName );
		
		if( exist != null && exist ){
			categoryExist = !categoryExist;
		}
		
		ResponseUtils.renderJson( response, String.valueOf( categoryExist ) );
		return;
	}
	
	/**
	 * 验证用户名
	 * @param username
	 * @param response
	 */
	@RequestMapping( "/v_check_username.jspx" )
	public void checkUsername( String username, HttpServletResponse response ){
		username = RequestUtils.parseParamUsingUTF8( username );

		if( StringUtils.isBlank( username ) ){
			ResponseUtils.renderJson( response, Boolean.FALSE.toString( ) );
			return;
		}
		ResponseUtils.renderJson( response, String.valueOf( manager.userNameNotExist( username )) );
		return;
	}
	
	@RequestMapping( "/v_check_email.jspx" )
	public void checkEmail( String email, HttpServletResponse response ) {
		if( StringUtils.isBlank( email ) ){
			ResponseUtils.renderJson( response, Boolean.FALSE.toString( ) );
			return;
		}
		ResponseUtils.renderJson( response, String.valueOf( manager.emailNotExist( email ) ) );
		return;
	}

	@RequestMapping( "/v_check_mobile.jspx" )
	public void checkMobile( String mobile, HttpServletResponse response ) {
		if( StringUtils.isBlank( mobile ) ){
			ResponseUtils.renderJson( response, Boolean.FALSE.toString( ) );
			return;
		}
		ResponseUtils.renderJson( response, String.valueOf( manager.mobileNotExist( mobile ) ) );
		return;
	}

	@RequestMapping( "/v_check_name.jspx" )
	public void checkName( String intermediaName, HttpServletRequest request, HttpServletResponse response ) {

		intermediaName = RequestUtils.parseParamUsingUTF8( intermediaName );

		if( StringUtils.isBlank( intermediaName ) ){
			ResponseUtils.renderJson( response, Boolean.FALSE.toString( ) );
			return;
		}

		ResponseUtils.renderJson( response, String.valueOf( manager.nameNotExist( intermediaName ) ) );
		return;
	}
	
	// ========================================= 
	
	@RequestMapping( "/v_user_check_username.jspx" )
	public void checkUsernameByUser( String username, HttpServletRequest request, HttpServletResponse response ){
		username = RequestUtils.parseParamUsingUTF8( username );

		if( StringUtils.isBlank( username ) ){
			ResponseUtils.renderJson( response, Boolean.FALSE.toString( ) );
			return;
		}

		ResponseUtils.renderJson( response, String.valueOf(  userMng.userNameNotExist( username ) ) );
		return;
	}
	
	@RequestMapping( "/v_user_check_mobile.jspx" )
	public void checkMobileByUser( String mobile, HttpServletRequest request, HttpServletResponse response ){
		mobile = RequestUtils.parseParamUsingUTF8( mobile );

		if( StringUtils.isBlank( mobile ) ){
			ResponseUtils.renderJson( response, Boolean.FALSE.toString( ) );
			return;
		}

		ResponseUtils.renderJson( response, String.valueOf(  userMng.mobileNotExist( mobile ) ) );
		return;
	}
	
	@RequestMapping( "/v_user_check_email.jspx" )
	public void checkEmailByUser( String email, HttpServletRequest request, HttpServletResponse response ){
		email = RequestUtils.parseParamUsingUTF8( email );

		if( StringUtils.isBlank( email ) ){
			ResponseUtils.renderJson( response, Boolean.FALSE.toString( ) );
			return;
		}

		ResponseUtils.renderJson( response, String.valueOf(  userMng.emailNotExist( email ) ) );
		return;
	}
	
	@RequestMapping( "/v_user_check_demoaccount.jspx" )
	public void checkDemoAccountByUser( String demoAccount, HttpServletRequest request, HttpServletResponse response ){
		demoAccount = RequestUtils.parseParamUsingUTF8( demoAccount );

		if( StringUtils.isBlank( demoAccount ) ){
			ResponseUtils.renderJson( response, Boolean.FALSE.toString( ) );
			return;
		}

		ResponseUtils.renderJson( response, String.valueOf(  userMng.demoAccountExit( demoAccount ) ) );
		return;
	}
	
	@RequestMapping( "/v_user_check_firmaccount.jspx" )
	public void checkFirmAccountByUser( String firmAccount, HttpServletRequest request, HttpServletResponse response ){
		firmAccount = RequestUtils.parseParamUsingUTF8( firmAccount );

		if( StringUtils.isBlank( firmAccount ) ){
			ResponseUtils.renderJson( response, Boolean.FALSE.toString( ) );
			return;
		}

		ResponseUtils.renderJson( response, String.valueOf(  userMng.firmAccountExit( firmAccount ) ) );
		return;
	}
	
	@Autowired
	private AdminMng manager;
	
	@Autowired 
	private ChannelMng channelMng;
	
	@Autowired
	private UserMng userMng;
	
	@Autowired
	private CategoryMng categoryMng;
	
	@Autowired
	private UserGroupMng groupMng;
	
	@Autowired
	private SmsSender smsSender;
	
	@Autowired
	private AgencyRoleMng agencyRoleMng; // 机构角色manager
	@Autowired
	private ProxyMng proxyMng; // 代理商manager
}
